<?php

class ImageController extends Controller
{
	public function actionIndex()
	{
		$this->render('index');
	}

	public function actionUpload(){
		
		$model = new PictureUploadForm;
		
		if(!empty($_FILES)){//upload

			$file = CUploadedFile::getInstanceByName('file');;

			$model->file = $file;
			if($model->validate()){
				
				$image = new Picture;
				$image->type = Picture::TYPE_PICTURE;
				$image->filename = $file->getTempName();
				if($image->save()){
					echo CJavaScript::encode(array('imgId'=>$image->_id.'','src'=>$this->createUrl('/picture/url',array('pid'=>$image->_id))));
					if(isset($_POST['imgId'])){
						@Picture::model()->deleteByPk(new MongoId($_POST['imgId']));
					}
				}else{
					echo json_encode(array('error'=>$image->getErrors()));
				}
				
			}else{
				
				echo json_encode(array('error'=>$model->getErrors()));
			}
			Yii::app()->end();
		}
		
		echo time();
		echo json_encode($_FILES);
		echo json_encode($_POST);
		Yii::app()->end();
	
	}
	// -----------------------------------------------------------
	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}